/*
  SOLID - Interference Detection Library
  Copyright (C) 1997 Gino van den Bergen

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

  Please send remarks, questions and bug reports to gino@win.tue.nl,
  or write to:
                  Gino van den Bergen
		  Department of Mathematics and Computing Science
		  Eindhoven University of Technology
		  P.O. Box 513, 5600 MB Eindhoven, The Netherlands
*/

#include "Shape.h"
#include "PointBuf.h"
#include "Transform.h"

#include <new.h>

Shape::~Shape() {
  for (iterator i = begin(); i != end(); ++i) delete *i;
}

/*
void printRec(BBoxTree *mytree) {
     BBox mybox;
     BBoxTree *mytreeleft;
     BBoxTree *mytreeright;
     if (mytree->isLeaf()) {
        // printf("leaf: %d\n",&mytree);
        mybox = mytree->volume();
        Point center = mybox.getCenter();
        Vector extent = mybox.getExtent();
        printf("%g\t%g\t%g\t%g\t%g\t%g\n",center[X],center[Y],center[Z],extent[X],extent[Y],extent[Z]);
     }
     else {
          mytreeleft = mytree->getLeft();
          // printf("left: %d\n",&mytreeleft);
          printRec(mytreeleft);
          mytreeright = mytree->getRight();
          // printf("right: %d\n",&mytreeright);
          printRec(mytreeright);
     }
}
*/

void printRec(BBoxTree *mytree) {
     BBox mybox;
     BBoxTree *mytreeleft;
     BBoxTree *mytreeright;

     if (mytree->isLeaf()) {}
     else {
          mytreeleft = mytree->getLeft();
          printRec(mytreeleft);
          mytreeright = mytree->getRight();
          printRec(mytreeright);
     }

     mybox = mytree->volume();
     Point center = mybox.getCenter();
     Vector extent = mybox.getExtent();
     printf("%g\t%g\t%g\t%g\t%g\t%g\n",center[X],center[Y],center[Z],extent[X],extent[Y],extent[Z]);
}

void printRecLeaf(BBoxTree *mytree) {
     BBox mybox;
     BBoxTree *mytreeleft;
     BBoxTree *mytreeright;

     if (mytree->isLeaf()) {

    const Polygon** Polys = mytree->getPolys();
    int i = 0;
    FILE *fp;
    fp=fopen("leaf.txt", "a+");
    fprintf(fp, "LEAF:\n");
    
    for (i = 0; i < mytree->getNPolys(); i++) {
        const Polygon* poly1 = Polys[i];
        Plane plneq1 = (poly1)->plane();
        Point* verts1 = (poly1)->getVertex();
        Vector norm1 = (&plneq1)->normal();
        Scalar dist1 = (&plneq1)->distance();
        fprintf(fp, "nverts: %d\n",(poly1)->size());
        fprintf(fp, "verts: %g\t%g\t%g\n%g\t%g\t%g\n%g\t%g\t%g\n%g\t%g\t%g\n",verts1[0][0],verts1[0][1],verts1[0][2],verts1[1][0],verts1[1][1],verts1[1][2],verts1[2][0],verts1[2][1],verts1[2][2],verts1[3][0],verts1[3][1],verts1[3][2]);
        fprintf(fp, "plane.norm:\t%g\t%g\t%g\nplane.dist:\t%g\n",norm1[X],norm1[Y],norm1[Z],dist1);
    }
    
    fclose(fp);
     
     }
     else {
          mytreeleft = mytree->getLeft();
          printRecLeaf(mytreeleft);
          mytreeright = mytree->getRight();
          printRecLeaf(mytreeright);
     }


}

void Shape::finish() {
    new(&tree) BBoxTree(size(), &front());
    printf("\nBoxTree - inizio\n");
    printRec(&tree);
    printf("BoxTree - fine\n");

    printf("\nBoxTree Leaf - inizio\n");
    printRecLeaf(&tree);
    printf("BoxTree Leaf - fine\n");
}

bool intersect(const Shape& a, const Shape& b, const Transform& t) {
  Matrix m = t.getOrientation().absolute();
  return intersect(a.tree, b.tree, t, m);
}

void intersect(const Shape& a, const Shape& b, const Transform& t, 
	       PointBuf& report) {
  Matrix m = t.getOrientation().absolute();
  intersect(a.tree, b.tree, t, m, report);
}



